Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 26, 2025

This PR refactors the JTD (JSON Type Definition) conversion logic as requested in the issue. Previously, each type class had its own toJtdForm() method implementation scattered across the codebase. This change centralizes all JTD conversion logic into a dedicated module with a clean router function.

Changes Made

Created centralized JTD converter module:

  • Added src/jtd/converter.ts with individual converter functions for each type (AnyType, BooleanType, ConstType, NumberType, StringType, ArrayType, ObjectType, TupleType, MapType, RefType, OrType, BinaryType, FunctionType)
  • Added main router function toJtdForm() that uses a switch statement to route each node type to its appropriate converter
  • Added src/jtd/index.ts to export the new converter functionality

Removed scattered toJtdForm() methods:

  • Removed method implementations from 6 type classes: AnyType, BooleanType, ConstType, NumberType, StringType (all had custom implementations)
  • Updated AbstractType.toJtdForm() to delegate to the centralized converter using dynamic import to avoid circular dependencies

Preserved exact functionality:

  • All existing behavior is maintained - the same JTD forms are generated for each type
  • Added comprehensive test suite (src/jtd/__tests__/converter.spec.ts) to verify all conversions work correctly
  • All existing tests continue to pass (488 tests passing)

The refactor improves code organization by following the single responsibility principle and makes the JTD conversion logic more maintainable by centralizing it in one location with a clear switch-based routing mechanism.

Fixes #25.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

… statement

Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor type toJtdForm() methods Refactor toJtdForm() methods to use centralized converter with switch statement Jul 26, 2025
Copilot AI requested a review from streamich July 26, 2025 23:08
Copilot finished work on behalf of streamich July 26, 2025 23:08
@streamich streamich marked this pull request as ready for review July 27, 2025 08:46
Copy link
Collaborator

@streamich streamich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run yarn prettier in the end to format codebase.

/**
* JTD form converter for BooleanType.
*/
export function convertBooleanType(type: BooleanType): jtd.JtdTypeForm {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inline all individual type converters, such as convertBooleanType, inside the main toJtdForm function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. All individual type converters have been inlined into the main toJtdForm function switch statement. (2088833)

Comment on lines 239 to 240
case 'bin':
return convertBinaryType(type as BinaryType<any>);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap each case in a block.

Suggested change
case 'bin':
return convertBinaryType(type as BinaryType<any>);
case 'bin': {
return convertBinaryType(type as BinaryType<any>);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Each case is now wrapped in a block with curly braces. (2088833)

Comment on lines 213 to 214
export function toJtdForm(type: AbstractType<any>): jtd.JtdForm {
const typeName = type.getTypeName();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this method work Schema instead, which is returned from AbstractType.getSchema().

Suggested change
export function toJtdForm(type: AbstractType<any>): jtd.JtdForm {
const typeName = type.getTypeName();
export function toJtdForm(schema: Schema): jtd.JtdForm {
const typeName = schema.kind;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. The method now works with Schema instead of AbstractType and uses schema.kind to determine the type. (2088833)

…s in blocks

Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 27, 2025

Run yarn prettier in the end to format codebase.

Done. Formatted the codebase using yarn format --write (Biome formatter). (2088833)

Copilot finished work on behalf of streamich July 27, 2025 09:12
Copilot AI requested a review from streamich July 27, 2025 09:12
Copy link
Collaborator

@streamich streamich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run yarn prettier in the end.

Comment on lines 47 to 64
switch (numSchema.format) {
case 'u8':
return {type: 'uint8'};
case 'u16':
return {type: 'uint16'};
case 'u32':
return {type: 'uint32'};
case 'i8':
return {type: 'int8'};
case 'i16':
return {type: 'int16'};
case 'i32':
return {type: 'int32'};
case 'f32':
return {type: 'float32'};
default:
return {type: 'float64'};
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it a map:

Suggested change
switch (numSchema.format) {
case 'u8':
return {type: 'uint8'};
case 'u16':
return {type: 'uint16'};
case 'u32':
return {type: 'uint32'};
case 'i8':
return {type: 'int8'};
case 'i16':
return {type: 'int16'};
case 'i32':
return {type: 'int32'};
case 'f32':
return {type: 'float32'};
default:
return {type: 'float64'};
}
return {type: NUMS_TYPE_MAPPING.get(numSchema.format) ?? 'float64'};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Replaced the switch statement with a Map lookup for cleaner code. (0da55d2)

Comment on lines 124 to 148
case 'or': {
// OrType is complex - for simplicity, convert to empty form
// In a full implementation, this might need to be a discriminator form
const form: jtd.JtdEmptyForm = {nullable: false};
return form;
}
case 'bin': {
// Binary types don't have a direct JTD equivalent, fall back to default
const form: jtd.JtdEmptyForm = {nullable: false};
return form;
}
case 'fn': {
// Function types don't have a direct JTD equivalent, fall back to default
const form: jtd.JtdEmptyForm = {nullable: false};
return form;
}
case 'fn$': {
// Streaming function types don't have a direct JTD equivalent, fall back to default
const form: jtd.JtdEmptyForm = {nullable: false};
return form;
}
default: {
const form: jtd.JtdEmptyForm = {nullable: false};
return form;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Remove comments.
  • Combine switch statements.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Removed comments and combined the switch statements for unsupported types (or, bin, fn, fn$) into a single case with fallthrough. (0da55d2)

…switch cases, and format with prettier

Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 27, 2025

Run yarn prettier in the end.

Done. Formatted the entire codebase using npx prettier --write. (0da55d2)

Copilot AI requested a review from streamich July 27, 2025 10:22
Copilot finished work on behalf of streamich July 27, 2025 10:22
Copy link
Collaborator

@streamich streamich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@streamich streamich merged commit 3bac6b2 into master Jul 27, 2025
2 checks passed
@streamich streamich deleted the copilot/fix-25 branch July 27, 2025 14:55
@github-actions
Copy link

🎉 This PR is included in version 1.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor type toJtdForm() methods

2 participants